package org.kernely.ui.profile;

import org.kernely.ui.MainApplication;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

/**
 * View of the user profile.
 */
public class KernelyProfileView extends VerticalLayout implements IKernelyView, KernelyEventSender {

	/**
	 * The Serial Version
	 */
	private static final long serialVersionUID = -5465377098197707204L;


	private VerticalLayout pictureLayout;

	

	private Embedded pictureEmbedded;
	private Upload uploadPicture;
	private Button resetPictureButton;
	
	private Button savePersoButton;
	private Button saveContactButton;
	private Button saveSocialButton;
	
	private TextField nameField;
	private TextField firstnameField;
	private TextField activityField;
	private TextArea descriptionField;
	
	private TextField emailField;
	private TextField skypeField;
	private TextField phoneField;
	private TextField addressField;
	
	private TextField viadeoField;
	private TextField linkedInField;
	private TextField twitterField;
	
	private NativeSelect civility;
	
	
	
	
	

	/**
	 * KernelyProfileView's constructor
	 */
	public KernelyProfileView(){
		
		Label title = new Label("Profile");
		title.setStyleName(Reindeer.LABEL_H1);
		title.setSizeUndefined(); // Usefull to center the label.
		
		// Personnal informations
		Label personnalTitle = new Label("Your personnal informations");
		personnalTitle.setStyleName(Reindeer.LABEL_H2);
		civility = new NativeSelect("Civility");
		civility.setNullSelectionAllowed(false);
		civility.addItem(0);
		civility.setItemCaption(0, "Mr.");
		civility.addItem(1);
		civility.setItemCaption(1, "Mrs.");
		civility.addItem(2);
		civility.setItemCaption(2, "Ms.");
		civility.setWidth(100, UNITS_PIXELS);
		nameField = new TextField("Name");
		nameField.setRequired(true);
		nameField.setRequiredError("Name can't be empty.");
		nameField.addValidator(new StringLengthValidator("Name is too long", 1, 50, false));
		firstnameField = new TextField("Firstname");
		firstnameField.setRequired(true);
		firstnameField.setRequiredError("Firstname can't be empty.");
		firstnameField.addValidator(new StringLengthValidator("Firstname is too long", 1, 50, false));
		activityField = new TextField("Activity");
		activityField.setNullRepresentation("");
		activityField.addValidator(new StringLengthValidator("Activity is too long", 0, 100, false));
		descriptionField = new TextArea("Who are you?");
		descriptionField.setNullRepresentation("");
		descriptionField.setRows(5);
		descriptionField.setColumns(30);
		descriptionField.addValidator(new StringLengthValidator("Description is too long", 0, 500, true));

		// Picture
		Label pictureTitle = new Label("Your picture");
		pictureTitle.setStyleName(Reindeer.LABEL_H2);
		pictureEmbedded = new Embedded();
		pictureEmbedded.setHeight(100,Sizeable.UNITS_PIXELS);
		pictureEmbedded.setWidth(100,Sizeable.UNITS_PIXELS);
		pictureEmbedded.setImmediate(true);
		uploadPicture = new Upload("Upload a new picture",null);
		resetPictureButton = new Button("Reset picture");

		
		// Contact
		Label contactTitle = new Label("Contact");
		contactTitle.setStyleName(Reindeer.LABEL_H2);
		emailField = new TextField("E-mail");
		emailField.setRequired(true);
		emailField.setRequiredError("E-mail can't be empty.");
		emailField.addValidator(new EmailValidator("This e-mail is not valid!"));		
		skypeField = new TextField("Skype");
		skypeField.setColumns(30);
		skypeField.setIcon(new ThemeResource("img/icon_skype.png"));
		skypeField.setNullRepresentation("");
		skypeField.addValidator(new StringLengthValidator("Skype is too long", 0, 50, true));
		phoneField = new TextField("Phone");
		phoneField.setNullRepresentation("");
		phoneField.addValidator(new StringLengthValidator("Phone number is too long", 0, 20, true));
		addressField = new TextField("Address");
		addressField.setColumns(30);
		addressField.setNullRepresentation("");
		addressField.addValidator(new StringLengthValidator("Address is too long", 0, 200, true));
		


		// Social networks
		
		String httpRegex = "(^http://.*|^$)*"; // Permits "http://anything" or "".
		
		Label socialTitle = new Label("Social networks");
		socialTitle.setStyleName(Reindeer.LABEL_H2);
		
		viadeoField = new TextField("Viadeo");
		viadeoField.setColumns(30);
		viadeoField.setIcon(new ThemeResource("img/icon_viadeo.png"));
		viadeoField.setNullRepresentation("");
		viadeoField.addValidator(new StringLengthValidator("Viadeo address is too long", 0, 50, true));
		viadeoField.addValidator(new RegexpValidator(httpRegex,"Viadeo address is incorrect"));
		linkedInField = new TextField("LinkedIn");
		linkedInField.setColumns(30);
		linkedInField.setIcon(new ThemeResource("img/icon_linkedin.png"));
		linkedInField.setNullRepresentation("");
		linkedInField.addValidator(new RegexpValidator(httpRegex,"LinkedIn address is incorrect"));
		linkedInField.addValidator(new StringLengthValidator("LinkedIn address is too long", 0, 50, true));
		twitterField = new TextField("Twitter");
		twitterField.setColumns(30);
		twitterField.setIcon(new ThemeResource("img/icon_twitter.png"));
		twitterField.setNullRepresentation("");
		twitterField.addValidator(new StringLengthValidator("Twitter login is too long", 0, 50, true));

		// Buttons
		
		savePersoButton = new Button("Save changes");
		saveContactButton = new Button("Save changes");
		saveSocialButton = new Button("Save changes");

		HorizontalLayout personnalInformationLayout = new HorizontalLayout();
		personnalInformationLayout.setSizeFull();
		
		VerticalLayout personnalFieldsLayout = new VerticalLayout();
		personnalFieldsLayout.addComponent(personnalTitle);
		personnalFieldsLayout.addComponent(new Label("<hr/>", Label.CONTENT_XHTML));
		personnalFieldsLayout.addComponent(civility);
		personnalFieldsLayout.addComponent(nameField);
		personnalFieldsLayout.addComponent(firstnameField);
		personnalFieldsLayout.addComponent(activityField);
		personnalFieldsLayout.addComponent(descriptionField);
		personnalFieldsLayout.addComponent(savePersoButton);
		personnalFieldsLayout.setSpacing(true);
		
		VerticalLayout personnalImageLayout = new VerticalLayout();
		personnalImageLayout.addComponent(pictureTitle);
		personnalImageLayout.addComponent(new Label("<hr/>", Label.CONTENT_XHTML));
		personnalImageLayout.addComponent(pictureEmbedded);
		personnalImageLayout.addComponent(uploadPicture);
		personnalImageLayout.addComponent(resetPictureButton);
		personnalImageLayout.setComponentAlignment(pictureEmbedded, Alignment.MIDDLE_CENTER);
		personnalImageLayout.setComponentAlignment(uploadPicture, Alignment.MIDDLE_CENTER);
		personnalImageLayout.setComponentAlignment(resetPictureButton, Alignment.MIDDLE_CENTER);
		personnalImageLayout.setSpacing(true);
		
		personnalInformationLayout.addComponent(personnalFieldsLayout);
		personnalInformationLayout.addComponent(personnalImageLayout);
		personnalInformationLayout.setMargin(true);
		
		VerticalLayout contactLayout = new VerticalLayout();
		contactLayout.setSizeFull();
		contactLayout.addComponent(contactTitle);
		contactLayout.addComponent(new Label("<hr/>", Label.CONTENT_XHTML));
		contactLayout.addComponent(emailField);
		contactLayout.addComponent(skypeField);
		contactLayout.addComponent(phoneField);
		contactLayout.addComponent(addressField);
		contactLayout.addComponent(saveContactButton);
		contactLayout.setSpacing(true);
		contactLayout.setMargin(true);
		
		VerticalLayout socialNetworkLayout = new VerticalLayout();
		socialNetworkLayout.setSizeFull();
		socialNetworkLayout.addComponent(socialTitle);
		socialNetworkLayout.addComponent(new Label("<hr/>", Label.CONTENT_XHTML));
		socialNetworkLayout.addComponent(viadeoField);
		socialNetworkLayout.addComponent(linkedInField);
		socialNetworkLayout.addComponent(twitterField);
		socialNetworkLayout.addComponent(saveSocialButton);
		socialNetworkLayout.setSpacing(true);
		socialNetworkLayout.setMargin(true);
				
		TabSheet profileTab = new TabSheet();
		profileTab.addTab(personnalInformationLayout,
		          "Personnal Informations", new ThemeResource("img/profile.png"));
		profileTab.addTab(contactLayout,
		          "Contact", new ThemeResource("img/contact.png"));
		profileTab.addTab(socialNetworkLayout,
		          "Social Network", new ThemeResource("img/socialNetwork.png"));
		
		this.addComponent(title);
		this.addComponent(profileTab);
		
		this.setComponentAlignment(title,Alignment.MIDDLE_CENTER);
		
		this.setSpacing(true);

	}

	/**
	 * This method is called when this component is added to an other.
	 */
	@Override
	public final void attach(){
		super.attach();
		//When the component is attached, we fire listeners
		MainApplication.getEventBus().broadcastEvent(new KernelyProfileAttachedEvent(this));
	}

	/**
	 * @see org.kernely.core.service.IKernelyView#getComponent()
	 */
	@Override
	public final Component getComponent() {
		return this;
	}

	/**
	 * Gets the embedded picture
	 * @return the pictureEmbedded
	 */
	public final Embedded getPictureEmbedded() {
		return pictureEmbedded;
	}

	/**
	 * Gets the uploadPicture
	 * @return the uploadPicture
	 */
	public final Upload getUploadPicture() {
		return uploadPicture;
	}

	/**
	 * Gets the resetPictureButton
	 * @return the resetPictureButton
	 */
	public final Button getResetPictureButton() {
		return resetPictureButton;
	}

	/**
	 * Gets the saveButton
	 * @return the saveButton
	 */
	public final Button getSavePersoButton() {
		return savePersoButton;
	}

	/**
	 * @return the saveContactButton
	 */
	public Button getSaveContactButton() {
		return saveContactButton;
	}

	/**
	 * @return the saveSocialButton
	 */
	public Button getSaveSocialButton() {
		return saveSocialButton;
	}

	/**
	 * Gets the nameField
	 * @return the nameField
	 */
	public final Field getNameField() {
		return nameField;
	}

	/**
	 * Gets the firstnameField
	 * @return the firstnameField
	 */
	public final Field getFirstnameField() {
		return firstnameField;
	}

	/**
	 * Gets the emailField
	 * @return the emailField
	 */
	public final Field getEmailField() {
		return emailField;
	}

	/**
	 * Gets the civility
	 * @return the civility
	 */
	public final NativeSelect getCivility() {
		return civility;
	}

	/**
	 * Gets the pictureLayout
	 * @return the pictureLayout
	 */
	public VerticalLayout getPictureLayout() {
		return pictureLayout;
	}

	/**
	 * Gets the skypeField
	 * @return the skypeField
	 */
	public TextField getSkypeField() {
		return skypeField;
	}

	/**
	 * Gets the phoneField
	 * @return the phoneField
	 */
	public TextField getPhoneField() {
		return phoneField;
	}

	/**
	 * Gets the addressField
	 * @return the addressField
	 */
	public TextField getAddressField() {
		return addressField;
	}

	/**
	 * Gets the viadeoField
	 * @return the viadeoField
	 */
	public TextField getViadeoField() {
		return viadeoField;
	}

	/**
	 * Gets the linkedInField
	 * @return the linkedInField
	 */
	public TextField getLinkedInField() {
		return linkedInField;
	}

	/**
	 * Gets the twitterField
	 * @return the twitterField
	 */
	public TextField getTwitterField() {
		return twitterField;
	}

	/**
	 * Gets the descriptionField
	 * @return the descriptionField
	 */
	public final TextArea getDescriptionField() {
		return descriptionField;
	}

	/**
	 * Gets the activityField
	 * @return the activityField
	 */
	public TextField getActivityField() {
		return activityField;
	}
	
	
	
	
}
